package com.xiaozhou.leetCode.leetCode100;

import java.util.*;
import java.util.stream.Collectors;

public class LeetCode17 {

    public static List<String> letterCombinations(String digits) {
        Map<Integer, List<String>> map = new HashMap();
        map.put(2, Arrays.asList("a", "b", "c"));
        map.put(3, Arrays.asList("d", "e", "f"));
        map.put(4, Arrays.asList("g", "h", "i"));
        map.put(5, Arrays.asList("j", "k", "l"));
        map.put(6, Arrays.asList("m", "n", "o"));
        map.put(7, Arrays.asList("p", "q", "r", "s"));
        map.put(8, Arrays.asList("t", "u", "v"));
        map.put(9, Arrays.asList("w", "x", "y", "z"));
        List<String> result = new ArrayList();
        if (digits.length() == 0) {
            return result;
        }
        result.addAll(map.get(digits.charAt(0) - '0'));
        for (int i = 1; i < digits.length(); i++) {
            int finalI = i;
            result = result.stream().flatMap(s -> map.get(digits.charAt(finalI) - '0').stream().map(s1 -> s + s1)).collect(Collectors.toList());
        }
        return result;
    }

    public static List<String> letterCombinations1(String digits) {
        Map<Integer, List<String>> map = new HashMap();
        map.put(2, Arrays.asList("a", "b", "c"));
        map.put(3, Arrays.asList("d", "e", "f"));
        map.put(4, Arrays.asList("g", "h", "i"));
        map.put(5, Arrays.asList("j", "k", "l"));
        map.put(6, Arrays.asList("m", "n", "o"));
        map.put(7, Arrays.asList("p", "q", "r", "s"));
        map.put(8, Arrays.asList("t", "u", "v"));
        map.put(9, Arrays.asList("w", "x", "y", "z"));

        Deque<String> deque = new ArrayDeque<>();
        if (digits.length() == 0){
            return new ArrayList();
        }
        deque.addAll(map.get(digits.charAt(0) - '0'));
        for (int i = 1; i < digits.length(); i++) {
            deque(deque, map, digits.charAt(i) - '0');
        }
        return new ArrayList(deque);
    }
    public static void deque(Deque<String> deque,Map<Integer,List<String>> map,Integer i){
        List<String> strings = map.get(i);
        if (strings == null){
            return;
        }
        for (String s : strings) {
            String poll = deque.poll();
            deque.add(poll + s);
        }
    }



    public static void main(String[] args) {
        List<String> strings = letterCombinations("23");
        System.out.println(strings);
    }
}
